package com.example.dbcms.service.impl;

import com.example.dbcms.domain.CostStatistic;
import com.example.dbcms.domain.FeeStatus;
import com.example.dbcms.domain.PropertyFee;
import com.example.dbcms.mapper.FeeStatusDao;
import com.example.dbcms.mapper.PropertyFeeDao;
import com.example.dbcms.mapper.ResidentDao;
import com.example.dbcms.service.PropertyFeeService;
import com.example.dbcms.utils.DateUtil;
import com.example.dbcms.utils.ToolUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PropertyFeeServiceImpl implements PropertyFeeService {

    @Autowired
    private PropertyFeeDao propertyFeeDao;

    @Autowired
    private ResidentDao residentDao;

    @Autowired
    private FeeStatusDao feeStatusDao;

    @Override
    public PageInfo<PropertyFee> selectAllPropertyFeeInfo(PropertyFee propertyFee) {
        PageHelper.startPage(propertyFee.getPageNum(), 10);
        PageInfo<PropertyFee> propertyFeePageInfo = new PageInfo<>(propertyFeeDao.selectAllPropertyFeeInfo(propertyFee));
        return propertyFeePageInfo;
    }

    @Override
    public List<PropertyFee> selectAllPropertyFeeInfoByCondition(Map<String, String> mapCondition) {
        return propertyFeeDao.selectAllPropertyFeeInfoByCondition(mapCondition);
    }

    @Override
    public int addPropertyFee(PropertyFee propertyFee) {
        propertyFee.setId(getMaxPropertyFeeId()+1);
        String buildingNo = propertyFee.getBuildingNo();
        String roomNo = propertyFee.getRoomNo();
        if(propertyFee.getWhether().equals("是")){
            propertyFee.setFeeDate(DateUtil.getCurrentDate());
            propertyFee.setOdd(ToolUtil.getRandomString(20));
        }
        propertyFee.setResidentId(residentDao.getResidentId(buildingNo,roomNo));
        return propertyFeeDao.addPropertyFee(propertyFee);
    }

    @Override
    public int addPropertyFee(int residentId, int year, int month, String fee) {
        PropertyFee propertyFee = new PropertyFee();
        propertyFee.setId(getMaxPropertyFeeId()+1);
        propertyFee.setResidentId(residentId);
        propertyFee.setYear(year);
        propertyFee.setMonth(month);
        propertyFee.setPropertyFee(fee);
        propertyFee.setWhether("否");
        return propertyFeeDao.addPropertyFee(propertyFee);
    }

    @Override
    public int deletePropertyFee(int id) {
        return propertyFeeDao.deletePropertyFee(id);
    }

    @Override
    public int getMaxPropertyFeeId() {
        return propertyFeeDao.getMaxPropertyFeeId();
    }

    @Override
    public PropertyFee getPropertyById(int id) {
        return propertyFeeDao.getPropertyById(id);
    }

    @Override
    public int updatePropertyFee(PropertyFee propertyFee) {
        propertyFee.setFeeDate(DateUtil.getCurrentDate());
        propertyFee.setOdd(ToolUtil.getRandomString(20));
        return propertyFeeDao.updatePropertyFee(propertyFee);
    }

    @Override
    public int deleteMultiplePropertyFee(List<Integer> ids) {
        return propertyFeeDao.deleteMultiplePropertyFee(ids);
    }

    @Override
    public int payMultiplePropertyFee(List<Integer> ids,String username) {
        for (int i=0;i<ids.size();i++){
            PropertyFee propertyFee = getPropertyById(ids.get(i));
            if(propertyFee.getWhether().equals("否")){
                propertyFee.setFeeDate(DateUtil.getCurrentDate());
                propertyFee.setWhether("是");
                propertyFee.setPeoName(username);
                propertyFee.setOdd(ToolUtil.getRandomString(20));
                propertyFeeDao.updatePropertyFee(propertyFee);
            }
        }
        return ids.size();
    }

    @Override
    public int payPropertyFee(int id,String username) {
        PropertyFee propertyFee = getPropertyById(id);
        if(propertyFee.getWhether().equals("否")){
            propertyFee.setFeeDate(DateUtil.getCurrentDate());
            propertyFee.setWhether("是");
            propertyFee.setPeoName(username);
            propertyFee.setOdd(ToolUtil.getRandomString(20));
            return propertyFeeDao.updatePropertyFee(propertyFee);
        }
        return -1;
    }

    @Override
    public CostStatistic getAllPropertyFee(CostStatistic costStatistic) {
        CostStatistic costStatistic1 = new CostStatistic();
        if(costStatistic.getBuildingNo()==null||costStatistic.getBuildingNo().length()==0){
            costStatistic.setBuildingNo(null);
        }

        if(costStatistic.getRoomNo()==null||costStatistic.getRoomNo().length()==0){
            costStatistic.setRoomNo(null);
        }

        if (costStatistic.getStartDate()==null||costStatistic.getStartDate().length()==0){
            costStatistic.setStartDate(null);
        }else{
            if(!DateUtil.isValidDate(costStatistic.getStartDate())) {
                costStatistic.setStartDate(null);
            }
        }
        if (costStatistic.getEndDate()==null||costStatistic.getEndDate().length()==0){
            costStatistic.setEndDate(null);
        }else {
            if (!DateUtil.isValidDate(costStatistic.getEndDate())) {
                costStatistic.setStartDate(null);
            }
        }
        int sum = 0; //费用总和
        int sumFeed = 0;  //已缴费总和
        int sumNotFeed = 0;  //未缴费总
        //先对日期田间进行查询
        if(costStatistic.getStartDate()==null||costStatistic.getEndDate()==null){
            List<PropertyFee> allPropertyFee = propertyFeeDao.getAllPropertyFee(costStatistic.getBuildingNo(),costStatistic.getRoomNo());
            for (PropertyFee propertyFee:allPropertyFee) {
                if(propertyFee.getWhether().equals("是")){
                    sumFeed+=Integer.parseInt(propertyFee.getPropertyFee().trim());
                }else {
                    sumNotFeed+=Integer.parseInt(propertyFee.getPropertyFee().trim());
                }
                sum+=Integer.parseInt(propertyFee.getPropertyFee().trim());
            }
            costStatistic1.setPropertyFeeValue(String.valueOf(sum),String.valueOf(sumFeed),String.valueOf(sumNotFeed));
        }else {
            //对日期进行遍历
            int yearStart = DateUtil.getYear(costStatistic.getStartDate());
            int monthStart = DateUtil.getMonth(costStatistic.getStartDate());
            int yearEnd = DateUtil.getYear(costStatistic.getEndDate());
            int monthEnd = DateUtil.getMonth(costStatistic.getEndDate());
            for (int i = yearStart;i<=yearEnd;i++){
                int startMonth,endMonth;
                if(i==yearStart&&i==yearEnd){
                    startMonth = monthStart;
                    endMonth = monthEnd;
                }else if(i==yearStart){
                    startMonth = monthStart;
                    endMonth = 12;
                }else if(i==yearEnd){
                    startMonth = 1;
                    endMonth = monthEnd;
                }else {
                    startMonth = 1;
                    endMonth = 12;
                }
                for(int j = startMonth;j<=endMonth;j++){
                    List<PropertyFee> allPropertyFeeByDate = propertyFeeDao.getAllPropertyFeeByDate(i, j,costStatistic.getBuildingNo(),costStatistic.getRoomNo());
                    // System.out.println(allPropertyFeeByDate);
                    for (PropertyFee propertyFee:allPropertyFeeByDate) {
                        if(propertyFee.getWhether().equals("是")){
                            sumFeed+=Integer.parseInt(propertyFee.getPropertyFee().trim());
                        }else {
                            sumNotFeed+=Integer.parseInt(propertyFee.getPropertyFee().trim());
                        }
                        sum+=Integer.parseInt(propertyFee.getPropertyFee().trim());
                    }
                }
            }
            costStatistic1.setPropertyFeeValue(String.valueOf(sum),String.valueOf(sumFeed),String.valueOf(sumNotFeed));
        }
//        System.out.println("所有物业费用总和"+sum);
//        System.out.println("所有已交物业费用总和"+sumFeed);
//        System.out.println("所有未缴物业费用总和"+sumNotFeed);
        return costStatistic1;
    }

    @Override
    public int remindPayPropertyFee() {
        List<Integer> residentIdList = residentDao.getResidentIdList(null, null);//获取全部居民信息
        for (int id:residentIdList) {
            List<Integer> propertyNotFeed = propertyFeeDao.getPropertyNotFeed(id);
            if(propertyNotFeed.size()>0){
                FeeStatus feeStatus = feeStatusDao.getFeeStatus(id);
                if(feeStatus==null){
                    FeeStatus feeStatusNew = new FeeStatus();
                    feeStatusNew.setId(feeStatusDao.getMaxId()+1);
                    feeStatusNew.setResidentId(id);
                    feeStatusNew.setParkingFeeStatus(0);
                    feeStatusNew.setPropertyFeeStatus(1);
                    feeStatusNew.setKeyWord("待缴费");
                    feeStatusDao.addFeeStatus(feeStatusNew);
                }else {
                    if(feeStatus.getPropertyFeeStatus()!=1){
                        feeStatus.setPropertyFeeStatus(1);
                        feeStatusDao.updatePropertyFeeStatus(feeStatus);
                    }
                }
            }else {
                //修改状态0
                FeeStatus feeStatus = feeStatusDao.getFeeStatus(id);
                if(feeStatus==null){
                    FeeStatus feeStatusNew = new FeeStatus();
                    feeStatusNew.setId(feeStatusDao.getMaxId()+1);
                    feeStatusNew.setResidentId(id);
                    feeStatusNew.setParkingFeeStatus(0);
                    feeStatusNew.setPropertyFeeStatus(0);
                    feeStatusNew.setKeyWord("待缴费");
                    feeStatusDao.addFeeStatus(feeStatusNew);
                }else {
                    if(feeStatus.getPropertyFeeStatus()!=1){
                        feeStatus.setPropertyFeeStatus(0);
                        feeStatusDao.updatePropertyFeeStatus(feeStatus);
                    }
                }
            }
        }
        return 0;
    }
}
